import {
  getBreadCrumbList,
  setTagNavListInLocalstorage,
  getMenuByRouter,
  getTagNavListFromLocalstorage,
  getHomeRoute,
  getNextRoute,
  routeHasExist,
  routeEqual,
  getRouteTitleHandled,
  localSave,
  localRead, getLoginUser, getSysConfig,setSysConfig
} from '@/libs/util'
import router from '@/router'
import routers from '@/router/routers'
import config from '@/config'
// import { saveErrorLogger } from '@/api/data'

const { homeName,dataHomeName } = config
const closePage = (state, route) => {
  //关闭页
/*  console.log(">>>>>>>>>>store.moudle.app.js.closePage: state:%o",state);
  console.log(">>>>>>>>>>store.moudle.app.js.closePage: route:%o",route);
  console.log(">>>>>>>>>>store.moudle.app.js.closePage: tagNavList:%o",state.tagNavList);*/
  const nextRoute = getNextRoute(state.tagNavList, route)
  state.tagNavList = state.tagNavList.filter(item => {
    return !routeEqual(item, route)
  })
  router.push(nextRoute)
}

export default {
  state: {
    breadCrumbList: [],
    tagNavList: [],
    homeRoute: {},
    local: localRead('local'),
    errorList: [],
    hasReadErrorPage: false,
    collapsedSideMenu: false, //是否已关闭左菜单
    sysConfig: getSysConfig(),
    //mapInfo:{}
  },
  getters: {
    menuList: (state, getters, rootState) => {
      //console.log("state.userInfo=%o",getLoginUser());
      return getMenuByRouter(routers, getLoginUser())
    },

    errorCount: state => state.errorList.length,
    getTagNavList: state =>state.tagNavList,
    getTagNavListCount: state =>state.tagNavList.length,
    getCollapsedSideMenu: state =>state.collapsedSideMenu,
    sysConfig: state => state.sysConfig,
    //mapInfo: state => state.mapInfo
  },
  mutations: {
    setSysConfig(state,sysConfig){
      setSysConfig(sysConfig)
      state.sysConfig = sysConfig;
    },
    setCollapsedSideMenu(state,closed){
      state.collapsedSideMenu = closed;
    },
    setBreadCrumb (state, route) {
      state.breadCrumbList = getBreadCrumbList(route, state.homeRoute)
    },
    setHomeRoute (state, routes) {
      //state.homeRoute = getHomeRoute(routes, homeName)
      state.homeRoute = getHomeRoute(routes, dataHomeName)
    },
    setTagNavList (state, list) {
/*      console.log(">>>>>>>>>>store.moudle.app.js.setTagNavList: homeName= %o",homeName);
      console.log(">>>>>>>>>>store.moudle.app.js.setTagNavList: list:=%o",list);*/
      let tagList = []
      if (list) {
        tagList = [...list]
      } else tagList = getTagNavListFromLocalstorage() || []

/*
      if (tagList[0] && tagList[0].name !== homeName) tagList.shift();
      let homeTagIndex = tagList.findIndex(item => item.name === homeName)
*/
      if (tagList[0] && tagList[0].name !== dataHomeName) tagList.shift();
      let homeTagIndex = tagList.findIndex(item => item.name === dataHomeName)


      if (homeTagIndex > 0) {
        let homeTag = tagList.splice(homeTagIndex, 1)[0]
        tagList.unshift(homeTag)
      }
      state.tagNavList = tagList;
      setTagNavListInLocalstorage([...tagList]);
    },
    closeTag (state, route) {
      //关闭标签
      let tag = state.tagNavList.filter(item => routeEqual(item, route))
      route = tag[0] ? tag[0] : null
      if (!route) return
      closePage(state, route)
    },
    addTag (state, { route, type = 'unshift' }) {
      let router = getRouteTitleHandled(route)
      if (!routeHasExist(state.tagNavList, router)) {
        if (type === 'push') {
          state.tagNavList.push(router);
        }
        else {
          //if (router.name === homeName) {
          if (router.name === dataHomeName) {
            state.tagNavList.unshift(router) //加在头部
          }else{
            state.tagNavList.splice(1, 0, router)
          }
        }
        setTagNavListInLocalstorage([...state.tagNavList])
      }
    },
    setLocal (state, lang) {
      localSave('local', lang)
      state.local = lang
    },
    addError (state, error) {
      state.errorList.push(error)
    },
    setHasReadErrorLoggerStatus (state, status = true) {
      state.hasReadErrorPage = status
    },

    closeSideMenu (state,closed) {
      state.collapsedSideMenu = closed;
    }

    //getMapInfo(state,mapInfo){state.mapInfo = mapInfo;}
  },
  actions: {
    addErrorLog ({ commit, rootState }, info) {
      console.log("addErrorLog: %o",info)
/*
      if (!window.location.href.includes('error_logger_page')) commit('setHasReadErrorLoggerStatus', false)
      const { user: { token, userId, userName } } = rootState
      let data = {
        ...info,
        time: Date.parse(new Date()),
        token,
        userId,
        userName
      }
      saveErrorLogger(info).then(() => {
        commit('addError', data)
      })
*/

    },
  }
}
